<template>
  <div class="question-container">
    <el-card>
      <el-form :inline="true" :model="query">
        <el-row>
          <el-col :span="20">
            <el-form-item label="关键字">
              <el-input
                placeholder=""
                size="small"
                v-model="query.keyword"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <el-button
                type="default"
                size="small"
                style="margin-right: 10px"
                @click="clear"
                >清除</el-button
              >
              <el-button type="primary" size="small" @click="search"
                >搜索</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据记录 -->
      <el-alert
        :title="`数据一共 ${total} 条`"
        style="margin-bottom: 15px"
        type="info"
        class="alert"
        :closable="false"
        show-icon
      ></el-alert>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="编号" width="200"> </el-table-column>
        <el-table-column prop="questionType" label="题型" width="50px">
          <template #default="{ row }">
            {{ GetquestionType(row.questionType) }}
          </template>
        </el-table-column>
        <el-table-column prop="questionIDs" label="题目编号" width="200px">
          <template #default="{ row }">
            <el-button
              class="btn"
              type="text"
              v-for="(item, index) in row.questionIDs"
              :key="index"
              @click="showDialog(item)"
              >{{ item.number }}</el-button
            >
          </template>
        </el-table-column>
        <el-table-column prop="addTime" label="录入时间" width="200px">
        </el-table-column>
        <el-table-column prop="totalSeconds" label="答题时间(s)">
        </el-table-column>
        <el-table-column prop="accuracyRate" label="正确率(%)">
        </el-table-column>
        <el-table-column prop="userName" label="录入人"> </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              circle
              @click="del(row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="end">
        <el-col :span="14">
          <el-pagination
            background
            style="margin-top: 20px"
            @size-change="changeSize"
            @current-change="changeCurrent"
            :current-page="query.page"
            :page-sizes="[1, 2, 3, 5, 20]"
            :page-size="query.pagesize"
            layout="prev, pager, next,sizes, jumper"
            :total="total"
          >
          </el-pagination>
        </el-col>
      </el-row>
    </el-card>

    <el-dialog
      title="题目预览"
      :visible="dialogVisible"
      width="60%"
      @close="closeDialog"
    >
      <QuestionsDetails ref="dialo" :dialogs="dialogs"></QuestionsDetails>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="closeDialog">关 闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { questionType } from '@/api/hmmm/constants'
import { randoms, removeRandoms, detail } from '@/api/hmmm/questions'
import QuestionsDetails from '@/module-hmmm/components/questions-details'
export default {
  components: {
    QuestionsDetails
  },
  data () {
    return {
      tableData: [],
      query: {
        page: 1,
        pagesize: 20,
        keyword: ''
      },
      total: 0,
      dialogVisible: false,
      dialogs: {}
    }
  },
  created () {
    this.getRandList()
  },
  methods: {
    closeDialog () {
      this.dialogVisible = false
      this.$refs.dialo.isshow = false
    },
    GetquestionType (type) {
      const value = questionType.find((v) => v.value === +type)
      return value.label
    },
    async showDialog (item) {
      // console.log(1)
      const res = await detail(item)
      // console.log(res)
      console.log(res)
      this.dialogVisible = true
      this.dialogs = res.data
      console.log(this.dialogs)
    },
    async del (item) {
      const res = await this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {})
      if (res !== 'confirm') return this.$message.info('已取消操作')
      await removeRandoms(item)
      if (this.tableData.length === 1 && this.query.page > 1) {
        this.query.page--
      }
      this.getRandList()
    },
    clear () {
      this.query.keyword = ''
    },
    search () {
      this.query.page = 1
      this.getRandList()
    },
    async getRandList () {
      const res = await randoms(this.query)
      console.log(res)
      this.tableData = res.data.items
      this.total = res.data.counts
    },
    changeCurrent (newPage) {
      this.query.page = newPage
      this.getRandList()
    },
    changeSize (newSize) {
      this.query.page = 1
      this.query.pagesize = newSize
      this.getRandList()
    }
  }
}
</script>

<style lang="scss">
.question-container {
  padding: 0 10px;
  margin: 10px 0;
}
.btn_wrapper {
  margin-bottom: 15px;
}
</style>
